package com.andbase.demo.activity;

import android.os.Build;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.andbase.demo.R;
import com.andbase.library.app.base.AbBaseActivity;

public class ScrollViewNestedWebViewActivity extends AbBaseActivity {

    private WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll_web_view);
        setToolbarView("ScrollView+WebView",true);
        initWebView();
    }

    public void initWebView() {
        webView = (WebView)this.findViewById(R.id.web_view);
        initWebViewSettings(webView);
        webView.loadUrl("https://blog.csdn.net/a724888/article/details/99709891");
    }


    @Override
    public void onBackPressed() {
        if(webView.canGoBack()){
            webView.goBack();
        }else{
            webView.destroy();
            finish();
        }
    }

    public void initWebViewSettings(WebView webView){
        try{
            WebSettings webSettings = webView.getSettings();
            webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
            webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
            webSettings.setUseWideViewPort(true);
            webSettings.setLoadWithOverviewMode(true);

            webSettings.setSupportMultipleWindows(false);

            //AppCache 已经不推荐使用了，标准也不会再支持
            webSettings.setAppCacheEnabled(false);
            webSettings.setGeolocationEnabled(false);
            webSettings.setAppCacheMaxSize(Long.MAX_VALUE);
            webSettings.setPluginState(WebSettings.PluginState.ON_DEMAND);
            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

            //设置支持JavaScript脚本
            webSettings.setJavaScriptEnabled(true);
            //设置可以访问文件
            webSettings.setAllowFileAccess(true);
            webSettings.setDomStorageEnabled(true);
            webSettings.setDefaultTextEncodingName("UTF-8");

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
                webSettings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            }

            //设置可以支持缩放
            webSettings.setSupportZoom(true);
            //设置默认缩放方式尺寸是far
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
            //设置出现缩放工具
            webSettings .setBuiltInZoomControls(false);
            webSettings.setDefaultFontSize(14);
            webSettings.setUserAgentString(webSettings.getUserAgentString() + "upu20.com");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
